//
//  HomePageTableViewController.m
//  GaoBiaoB
//
//  Created by lanou3g on 16/1/15.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "HomePageTableViewController.h"
#import "HomeTableViewCell.h"
#import "HomePageCollectionViewCell.h"
#import "HomePageDataManager.h"
//#import "HomeCatrgeryModel.h"
#import "SDCycleScrollView.h"
#import "UIImageView+WebCache.h"
<<<<<<< HEAD
//#import "GuessModel.h"
#import "XuanCityTableViewController.h"
#import "SmallCity.h"
#import "CategoryTableViewController.h"
#import "Nuomishuo.h"
#import "ChaShop.h"
#import "ChaXtuan.h"
//#import "TuandanFenglei.h"
@interface HomePageTableViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UIScrollViewDelegate,SDCycleScrollViewDelegate>

=======
#import "GuessModel.h"

@interface HomePageTableViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UIScrollViewDelegate>
{
    NSArray *_arrayBan;
}
>>>>>>> 7ba6d124b0e065df0ef7b447a63d726a6c3dcba9
//展示轮播图的家伙
@property(nonatomic,strong)UIView *headerView;
//集合视图
@property(nonatomic,strong)UICollectionView *collectView;

<<<<<<< HEAD
//接收数据
@property (nonatomic,strong)NSMutableArray *otherArray;


//展示店面列表信息
@property(nonatomic,strong)NSMutableArray * listArray;
@property(nonatomic,strong)NSMutableDictionary *listDict;

=======
>>>>>>> 7ba6d124b0e065df0ef7b447a63d726a6c3dcba9
@end

//重用标识
NSString *cellIdentifier = @"homePageCell";
NSString *collectionViewIdentifier = @"LGJcolloctionView";
@implementation HomePageTableViewController
<<<<<<< HEAD
=======

>>>>>>> 7ba6d124b0e065df0ef7b447a63d726a6c3dcba9
- (void)viewDidLoad {
    [super viewDidLoad];
    [self basicSetUp];
    [self aHeaderView];
    
    //自定义cell注册
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeTableViewCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];

    //集合视图注册
    [self.collectView registerNib:[UINib nibWithNibName:@"HomePageCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:collectionViewIdentifier];
    
    [HomePageDataManager sharedHomeDataManger].block = ^(){
        [self.collectView reloadData];
        [self.tableView reloadData];
    };
    [[Nuomishuo alloc]init].upData = ^(){
        [self.collectView reloadData];
        [self.tableView reloadData];
    };
}
//基础信息设置ß
-(void)basicSetUp{
<<<<<<< HEAD
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:0.99];;
    self.title = _model.biz_area_name;
=======
    self.view.backgroundColor = [UIColor whiteColor];
>>>>>>> 7ba6d124b0e065df0ef7b447a63d726a6c3dcba9
    self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-erweima"] style:UIBarButtonItemStyleDone target:self action:@selector(qrCodeDidClicked:)],[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-gouwuche"] style:UIBarButtonItemStyleDone target:self action:@selector(shoppingCarDidClicked:)]];
    UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(60, 10, 80, 25)];
//    lb.backgroundColor =[UIColor purpleColor];
    lb.text= @"北京";
    [self.navigationController.navigationBar addSubview:lb];
    self.navigationItem.leftBarButtonItems = @[[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-xiao"] style:UIBarButtonItemStyleDone target:self action:@selector(citySelect:)]];
}
-(void)citySelect:(UIBarButtonItem *)sender{
    
}
//上部视图
-(void)aHeaderView{
    self.headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width/ 5 * 4)];
    _headerView.backgroundColor = [UIColor purpleColor];
    _headerView.tag =101;
    self.tableView.tableHeaderView = _headerView;
    [self createCollectionView];
}
//轮播图下面类目选择
-(void)createCollectionView{
    //轮播图实现
    NSArray *images = @[[UIImage imageNamed:@"h1.jpg"],
                        [UIImage imageNamed:@"h2.jpg"],
                        [UIImage imageNamed:@"h3.jpg"],
                        [UIImage imageNamed:@"h4.jpg"]
                        ];
    CGFloat x = self.view.bounds.size.width;
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, x, self.headerView.frame.size.height/3*2) imagesGroup:images];
    cycleScrollView.infiniteLoop = YES;
    cycleScrollView.delegate = self;
    cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    [self.headerView addSubview:cycleScrollView];
    //         --- 轮播时间间隔，默认1.0秒，可自定义
    cycleScrollView.autoScrollTimeInterval = 4.0;
/*
    //网络加载 --- 创建带标题的图片轮播器
    UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 320, w, 180)];
    v1.backgroundColor = [UIColor whiteColor];
    SDCycleScrollView *cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 320, w, 180) imageURLStringsGroup:nil]; // 模拟网络延时情景
    cycleScrollView2.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    cycleScrollView2.delegate = self;
    cycleScrollView2.titlesGroup = titles;
    cycleScrollView2.dotColor = [UIColor yellowColor]; // 自定义分页控件小圆标颜色
    cycleScrollView2.placeholderImage = [UIImage imageNamed:@"placeholder"];
    [v1 addSubview:cycleScrollView2];
    [self.view addSubview:v1];
    
    //             --- 模拟加载延迟
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        cycleScrollView2.imageURLStringsGroup = imagesURLStrings;
    });

    */
    //集合视图展示
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 5;
    flowLayout.itemSize = CGSizeMake(100, 100);
    self.collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.headerView.frame.size.height/3*2, self.headerView.frame.size.width, self.headerView.frame.size.height/3) collectionViewLayout:flowLayout];
    self.collectView.backgroundColor = [UIColor whiteColor];
    [self.headerView addSubview:self.collectView];
    self.collectView.dataSource = self;
    self.collectView.delegate = self;
}
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", index);
}

//点击购物车触发事件
-(void)shoppingCarDidClicked:(UIBarButtonItem *)sender{

}
//扫描二维码触发事件
-(void)qrCodeDidClicked:(UIBarButtonItem *)sender{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
<<<<<<< HEAD
    
    return [_listDict[_listArray[section]]count];
=======
    return [HomePageDataManager sharedHomeDataManger].guessYouLikeArr.count;
>>>>>>> 7ba6d124b0e065df0ef7b447a63d726a6c3dcba9
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSArray *array = _listDict[_listArray[indexPath.section]];
    ChaXtuan *model = array[indexPath.row];
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
<<<<<<< HEAD
    cell.homePageNameLabel.text = model.title;
    cell.homePagePriceLabel.text = [NSString stringWithFormat:@"￥ %d",model.current_price/100];
    [cell.homepageImageView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"iconfont-tu"]];
    cell.homePageDistanceLabel.text = [NSString stringWithFormat:@"%d人已购买",model.sale_num];
    return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [NSString stringWithFormat:@"%@",_listArray[section]];
}
=======
    cell.homePageNameLabel.text  = model.brand_name;
    cell.homePagePriceLabel.text =[NSString stringWithFormat:@"￥ %ld", [model.groupon_price integerValue]/100];
    cell.homePageDistanceLabel.text = model.distance;
//    [cell.homepageImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.aImage]] placeholderImage:[UIImage imageNamed:@"iconfont-tu"]];
    return cell;
}
>>>>>>> 7ba6d124b0e065df0ef7b447a63d726a6c3dcba9

#pragma mark----colloctionView dataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [HomePageDataManager sharedHomeDataManger].categoryNameArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomePageDataManager *manager =[HomePageDataManager sharedHomeDataManger];
    NSArray *array = manager.categoryNameArray;
    NSString *str  =array[indexPath.row];
    HomePageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewIdentifier forIndexPath:indexPath];
<<<<<<< HEAD
    cell.aImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",indexPath.row + 1]];
    cell.categoryLabel.text = str;
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CategoryTableViewController *category =[[CategoryTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
    
    //吧当前页的城市id传递出去===========
    category.category_City = _midCityID;

    //把当前页的二级城市id传出去
    category.category_SubCity = _smallCityID;
    
    //点击的下标传递出去
    category.category_ID = (int)indexPath.row;
    //==============================
    
    [self presentViewController:category animated:YES completion:^{
    }];
}

#pragma mark------解析店面列表数据
-(void)requestNet{

    if (_midCityID == nil ) {
        _midCityID = @"100010000";
    }
    if (_smallCityID == nil ) {
        _smallCityID = @"394395";
    }
    NSString *httpStr = @"http://apis.baidu.com/baidunuomi/openapi/searchshops";
    NSString *urlStr = [NSString stringWithFormat:@"city_id=%@&cat_ids=&subcat_ids=&district_ids=&bizarea_ids=&location=&keyword=&radius=&page=&page_size=&deals_per_shop=",_midCityID];
    Nuomishuo *nuo= [[Nuomishuo alloc]init];
        [nuo request:httpStr withHttpArg:urlStr];
        _otherArray = [NSMutableArray array];
        __weak typeof(nuo)temp = nuo;
        dispatch_async(dispatch_get_main_queue(), ^{
            temp.upData=^(){
                _otherArray = nuo.array;
                for (ChaShop * cha in temp.array) {
                    NSString *key = cha.shop_name;
                    for (NSDictionary *small in cha.deals) {
                        ChaXtuan *model = [ChaXtuan new];
                        [model setValuesForKeysWithDictionary:small];
                        //要是组不存在那么重新创建
                        if (self.listDict[key] == nil) {
                            NSMutableArray *groupArray = [NSMutableArray array];
                            [groupArray addObject:model];
                            [self.listDict setObject:groupArray forKey:key];
                        }else{
                            NSMutableArray *groupArray = self.listDict[key];
                            [groupArray addObject:model];
                        }
                    }
                }
                self.listArray = [NSMutableArray arrayWithArray:_listDict.allKeys];
//                NSLog(@"%@",_listArray);
                [self.tableView reloadData];
            };
        });
}
-(NSMutableDictionary *)listDict{
    if (_listDict == nil) {
        _listDict = [NSMutableDictionary dictionary];
    }
    return _listDict;
=======
    [cell.aImageView sd_setImageWithURL:[NSURL URLWithString:model.category_picurl] placeholderImage:[UIImage imageNamed:@"iconfont-erweima"]];
    cell.categoryLabel.text = model.category_name;
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
>>>>>>> 7ba6d124b0e065df0ef7b447a63d726a6c3dcba9
}
*/


@end
